package com.zds.support.widget

import android.content.Context
import android.util.AttributeSet
import androidx.annotation.DrawableRes
import androidx.appcompat.widget.AppCompatImageView
import com.zds.support.R
import com.zds.support.gilde.ImageLoaderManager

/**
 *
 * @Description: 图片显示控件，替换ImageView，以防后期替换图片加载控件需要大量更改原来的代码
 * @author zc
 * @version V1.0
 *
 */

open class AppImageView @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null,
        defStyleAttr: Int = 0
) : AppCompatImageView(context, attrs, defStyleAttr) {

    /**
     * 加载图片
     * 支持HTTP/HTTPS网络图片
     * 支持asset资源（"file:///android_asset/test.png"）
     * 支持SD卡资源（"file://sdcard/test.jpg"）
     * 支持raw资源（"android.resource://包名/raw/文件名"）或者（"android.resource://包名/raw/"+R.raw.test）
     * 支持drawable资源（"android.resource://包名/drawable/文件名"）或者（"android.resource://包名/drawable/"+R.drawable.test）
     * 支持ContentProvider资源（"content://media/external/images/media/139469"）
     */
    fun loadImage(path: String) {
        loadImage(path, R.drawable.bg_image, R.drawable.bg_image)
    }

    fun loadImage(path: String,
                  onSuccessListener: (path: String) -> Unit,
                  onFailListener: (e: Exception?) -> Unit) {
        loadImage(path, R.drawable.bg_image, R.drawable.bg_image, onSuccessListener, onFailListener)
    }

    fun loadImageCorner(path: String, cornerSize: Int) {
        loadImageCorner(path, R.drawable.bg_image, R.drawable.bg_image, cornerSize)
    }

    fun loadImageWithoutPlaceholder(path: String) {
        ImageLoaderManager.instance.showImage(this, path)
    }

    fun loadImage(path: String, @DrawableRes placeholderResId: Int, @DrawableRes failureResId: Int) {
        /*  if (ImageLoaderManager.instance.baseImageUrl.equals(path)) {
              loadFail(failureResId)
          } else {
              ImageLoaderManager.instance.showImage(this, path, placeholderResId, failureResId)
          }
  */
        ImageLoaderManager.instance.showImage(this, path, placeholderResId, failureResId)
    }

    fun loadImage(path: String, @DrawableRes placeholderResId: Int, @DrawableRes failureResId: Int,
                  onSuccessListener: (path: String) -> Unit,
                  onFailListener: (e: Exception?) -> Unit) {

        ImageLoaderManager.instance.showImage(this, path, placeholderResId, failureResId,
                onSuccessListener, onFailListener)
    }

    fun loadGif(path: String) {
        ImageLoaderManager.instance.loadGif(this, path)
    }

    fun loadImageCorner(path: String, @DrawableRes placeholderResId: Int, @DrawableRes failureResId: Int, cornerSize: Int) {
        ImageLoaderManager.instance.showImageCorner(this, path, placeholderResId, failureResId, cornerSize)
    }

    fun loadFail() {
        loadFail(R.drawable.bg_image)
    }

    fun loadFail(@DrawableRes failureResId: Int) {
        setImageResource(failureResId)
    }

    /*override fun setOnClickListener(l: OnClickListener?) {
        if (l == null) {
            super.setOnClickListener(l)
        } else {
            super.setOnClickListener(object : OnClickListener {
                override fun onClick(v: View?) {
                    if (!FastClickUtils.isFastClick()) {
                        l.onClick(v)
                    }
                }
            })
        }

    }*/
}